package com.adminsystem.java.devlogs.service.impl;

import com.adminsystem.java.devlogs.mapper.DevLogMapper;
import com.adminsystem.java.devlogs.pojo.entity.DevLog;
import com.adminsystem.java.devlogs.pojo.params.DevLogAddParam;
import com.adminsystem.java.devlogs.pojo.vo.DevLogVO;
import com.adminsystem.java.devlogs.service.DevLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangpeiao
 */
@Service
public class DevLogServiceImpl implements DevLogService {

    private final DevLogMapper devLogMapper;

    public DevLogServiceImpl(DevLogMapper devLogMapper) {
        this.devLogMapper = devLogMapper;
    }

    @Override
    public List<DevLogVO> getAllDevLogs() {
        List<DevLog> logs = devLogMapper.getAllDevLogs();

        return logs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    @Override
    public boolean addDevLog(DevLogAddParam param) {
        DevLog devLog = new DevLog();
        devLog.setVersion(param.getVersion());
        devLog.setChangeType(param.getChangeType());
        devLog.setModule(param.getModule());
        devLog.setCategory(param.getCategory());
        devLog.setTitle(param.getTitle());
        devLog.setContent(param.getContent());
        devLog.setLogTime(param.getLogTime() != null ? param.getLogTime() : LocalDateTime.now());
        int rows = devLogMapper.insertDevLog(devLog);
        return rows > 0;
    }
    private DevLogVO convertToVO(DevLog devLog) {
        DevLogVO vo = new DevLogVO();
        vo.setId(devLog.getId());
        vo.setVersion(devLog.getVersion());
        vo.setChangeType(devLog.getChangeType());
        vo.setModule(devLog.getModule());
        vo.setCategory(devLog.getCategory());
        vo.setTitle(devLog.getTitle());
        vo.setContent(devLog.getContent());
        vo.setLogTime(devLog.getLogTime());
        return vo;
    }
}
